package com.han.hotelplat.service.datacenter.hotel.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.model.AreaModel;
import com.han.hotelplat.service.datacenter.hotel.HotelDataService;

@Service
public class HotelDataServiceImpl implements HotelDataService{
	@Autowired
	private  EntityManage entityManage;

	@Override
	public Map<String, Integer> getHotelDataByState() {
		int sumCount = 0;//总数
		int passCount = 0;//通过的数量
		int waitCount = 0;//待审核的数量
		int noPassCount = 0;//未通过的数量
		int freezeCount = 0;//冻结的数量
		String sql = "SELECT h.state ,COUNT(h.id) FROM tb_hotel AS h GROUP BY h.state";
		List<Object> ctli = entityManage.findBySql(sql.toString());
		for(int i= 0 ; i<ctli.size(); i++){
			Object[] ory = (Object[]) ctli.get(i);
			int state = -1;//状态
			int count = 0;//人数
			if(ory[0] != null){//状态
				state = Integer.parseInt(ory[0].toString());
			}
			if(ory[1] != null){//人数
				count = Integer.parseInt(ory[1].toString());
			}
			sumCount += count;
			if(state == 1){
				waitCount = count;
			}
			if(state == 2){
				passCount = count;
			}
			if(state == 3){
				noPassCount = count;
			}
			if(state == 4){
				freezeCount = count;
			}
		}
		Map<String, Integer> data = new HashMap<String, Integer>();
		data.put("sumCount", sumCount);
		data.put("waitCount", waitCount);
		data.put("passCount", passCount);
		data.put("noPassCount", noPassCount);
		data.put("freezeCount", freezeCount);
		return data;
	}

	@Override
	public List<Map<String, Object>> getHotelDataByArea(Integer groupArea) {
		String aCode = "substr(h.area,1,4 )";
		if(groupArea != null && groupArea.intValue() == 1){//按市级归类
			aCode = "substr(h.area,1,2)";
		}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
			aCode = "substr(h.area,1,4)";
		}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
			aCode = "substr(h.area,1,6)";
		}
		List<Map<String, Object>> returnData = new ArrayList<Map<String,Object>>();
		String sql = "SELECT "+aCode+", COUNT(h.id) as hcount FROM tb_hotel AS h group by "+aCode+" order by hcount ASC";
		List<Object> ctli = entityManage.findBySql(sql.toString());
		Map<String, Integer> data = new HashMap<String, Integer>();
		for(int i= 0 ; i<ctli.size(); i++){
			Object[] ory = (Object[]) ctli.get(i);
			String areaName = "未知";
			Integer count = 0;
			if(ory[0] != null){//状态
				Integer area = 0;
				String code = ory[0].toString();
				if(groupArea != null && groupArea.intValue() == 1){//按省级归类
					area = Integer.parseInt(code)*10000;
					areaName= AreaHelp.getInstance().getProvince(area);
				}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
					area = Integer.parseInt(code)*100;
					areaName = AreaHelp.getInstance().getCity(area);
				}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
					area = Integer.parseInt(code);
					areaName = AreaHelp.getInstance().getAreaModel(code).getName();
				}
			}
			if(ory[1] != null){//数量
				count = Integer.parseInt(ory[1].toString());
			}
			if("未知".equals(areaName) && data.containsKey(areaName)){
				data.put(areaName, count+data.get(areaName));
			}else{
				data.put(areaName, count);
			}
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("areaName", areaName);
			sub.put("count", data.get(areaName));
			returnData.add(sub);
		}
		/*for(Map.Entry<String, Integer> en : data.entrySet()){
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("areaName", en.getKey());
			sub.put("count", en.getValue());
			returnData.add(sub);
		}*/
		return returnData;
	}

	@Override
	public Map<String, Integer> getHotelDataByHoteType() {
		int one = 0;//其他
		int two = 0;//连锁
		int three = 0;//三星
		int four = 0;//四星
		int five = 0;//五星
		String sql = "SELECT h.hoteltype, COUNT(h.id) FROM tb_hotel AS h GROUP BY h.hoteltype";
		List<Object> ctli = entityManage.findBySql(sql.toString());
		for(int i= 0 ; i<ctli.size(); i++){
			Object[] ory = (Object[]) ctli.get(i);
			int type = 0;//类型
			int count = 0;//数量
			if(ory[0] != null){//类型
				type = Integer.parseInt(ory[0].toString());
			}
			if(ory[1] != null){//数量
				count = Integer.parseInt(ory[1].toString());
			}
			if(type == 1){
				one = count;
			}else if(type == 2){
				two = count;
			}else if(type == 3){
				three = count;
			}else if(type == 4){
				four = count;
			}else if(type == 5){
				five = count;
			}
		}
		Map<String, Integer> data = new HashMap<String, Integer>();
		data.put("其他", one);
		data.put("连锁酒店", two);
		data.put("三星级酒店", three);
		data.put("四星级酒店", four);
		data.put("五星级酒店", five);
		return data;
	}

	@Override
	public Map<String, Integer> getNewHotelDataGroupDate(String startTime,
			String endTime, Integer areaCode) {
		StringBuffer sql = new StringBuffer("SELECT substr(h.creattime,1,10), COUNT(h.id)FROM tb_hotel AS h WHERE 1=1 ");
		if(StringUtils.isNotBlank(startTime)){
			sql.append(" AND h.creattime >= '"+startTime+"'");
		}
		if(StringUtils.isNotBlank(endTime)){
			sql.append(" AND h.creattime <= '"+endTime+" 23:59:59'");
		}
		if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+areaCode +" <= "+x;
			sql.append(w_area);
		}
		
		sql.append(" GROUP BY substr(h.creattime,1,10)");
		List<Object> ctli = entityManage.findBySql(sql.toString());
		TreeMap<String, Integer> data = new TreeMap<String, Integer>();
		for(int i= 0 ; i<ctli.size(); i++){
			Object[] ory = (Object[]) ctli.get(i);
			String key = "";
			int count = 0;
			if(ory[0] != null){
				key = ory[0].toString();//日期
			}
			if(ory[1] != null){//人数
				count = Integer.parseInt(ory[1].toString());
			}
			data.put(key, count);
		}
		return data;
	}
}
